<?php
/**
 * 用户信息表 模型
 * Created by PhpStorm.
 * User: chen
 * Date: 2016/9/23
 * Time: 9:34
 */
namespace app\common\model;
use think\Model;
use think\Request;//请求类
use think\Session;//session类
class Userinfo extends Model {

    /**
     * 定义一对一关联方法  belongsTo('关联模型名','关联外键','关联模型主键','别名定义','join类型')
     */
    public function userinfo() {
        //在User模型中 我们定义了 hasOne('Userinfo','uid') 关联方法,如果你不需要操作User话此处可以需要关联方法
        //操作的话,必须建立对应的操作方法
        return $this->belongsTo('User');
    }

    /**
     * 执行用户信息 更新 | 新增
     */
    public function send_userinfo() {
        $request = Request::instance();
        $id = $request->post('id');
        //组合数据
        $data = [
            'username' => trim($request->post('username')),
            'truename' => trim($request->post('truename')),
            'sex' => trim($request->post('sex')),
            'location' => trim($request->post('prov')) . "_" . trim($request->post('city')) . "_" . trim($request->post('dist')),
            'constellation' => trim($request->post('constellation')),
            'intro' => trim($request->post('intro')),
        ];
        if ($id) {
            //编辑操作
            $this->isUpdate(true)->save($data,['uid'=>Session::get('uid')]);
            return true;
        } else {
            //新增操作
            if ($this->isUpdate(false)->save($data)) {
                return true;
            } else {
                $this->error = '系统错误,请重试...';
                return false;
            }
        }
    }


    /**
     * 获取用户数据
     */
    public function get_find_data() {
        $userinfo = $this->where(['uid' => Session::get('uid')])->find();
        if ($userinfo->uid) {
            $data = [
                'id'                => $userinfo->id,
                'username'          => $userinfo->username,
                'truename'          => $userinfo->truename,
                'sex'               => $userinfo->sex,
                'location'          => $userinfo->location,
                'constellation'     => $userinfo->constellation,
                'intro'             => $userinfo->intro,
                'uid'               => $userinfo->uid,
                'face180'           => $userinfo->face180,
                'face'              => $userinfo->face,
            ];
            if ($userinfo->location) {
                $area = explode('_',$userinfo->location);
                if (count($area) == 2) {
                    $data['prov'] = $area[0];
                    $data['city'] = $area[1];
                    $data['dist'] = null;
                } else if (count($area) == 3) {
                    $data['prov'] = $area[0];
                    $data['city'] = $area[1];
                    $data['dist'] = $area[2];
                } else {
                    $data['prov'] = $area[0];
                    $data['city'] = null;
                    $data['dist'] = null;
                }
            } else {
                $data['prov'] = '广东';
                $data['city'] = '深圳';
                $data['dist'] = '罗湖区';
            }
            return $data;
        } else {
            return [];
        }
    }
}